/*
 * Created by JFormDesigner on Sun Jan 30 04:56:55 ART 2011
 */

package ar.com.visualAORE.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.ResourceBundle;
import javax.swing.*;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Highlighter;

import ar.com.visualAORE.controller.VersionControlFrameController;
import ar.com.visualAORE.model.Document;
import ar.com.visualAORE.model.Selection;
import ar.com.visualAORE.util.UnderlineHighlightPainter;

import com.jgoodies.forms.factories.DefaultComponentFactory;

/**
 * @author julian
 */
public class VersionControlFrame extends JFrame {
	VisualDesktopFrame vdf;
	Document document;
	Highlighter highlighter;
	
	public VersionControlFrame() {
		initComponents();
	}
	public VersionControlFrame(VisualDesktopFrame vdf, VersionControlFrameController controller){
		initComponents();
		this.vdf = vdf;
		this.highlighter = textPaneLastDocumentSaved.getHighlighter();
		this.setVisible(true);
	}
	/**
	 * Pinta un requerimiento a partir de su archivo xml
	 * @param document
	 */
	public void applyDocumentLayer(Document document){
		this.document = document;
		this.setTitle(document.getName());
		Iterator<Selection> it = document.getSelections().iterator();
		while(it.hasNext()){
			Selection selection = it.next();
			vdf.setPainter(new UnderlineHighlightPainter(selection.getConcern().getColor()));	
			try {
				highlighter.addHighlight(selection.getDot(), selection.getMark(), vdf.getPainter());
			} catch (BadLocationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	   * Return an int containing the wrapped line index at the given position
	   * @param component JTextPane
	   * @param int pos
	   * @return int
	   */
	  public int getLineNumber(JTextPane component, int pos) 
	  {
	    int posLine;
	    int y = 0;

	    try
	    {
	      Rectangle caretCoords = component.modelToView(pos);
	      y = (int) caretCoords.getY();
	    }
	    catch (BadLocationException ex)
	    {
	    }

	    int lineHeight = component.getFontMetrics(component.getFont()).getHeight();
	    posLine = (y / lineHeight) + 1;
	    return posLine;
	  }
	private void textPaneLastDocumentSavedCaretUpdate(CaretEvent e) {
		labelVersionControlLNLast.setText("Line Number: " + getLineNumber(textPaneLastDocumentSaved, textPaneLastDocumentSaved.getCaretPosition()));
	}

	private void textPaneNewDocumentCaretUpdate(CaretEvent e) {
		labelVersionControlNew.setText("Line Number: " + getLineNumber(textPaneNewDocument, textPaneNewDocument.getCaretPosition()));
	}
	public JTextPane getJtextPaneLastDocumentSaved(){
		return this.textPaneLastDocumentSaved;
	}
	public JTextPane getJtextPaneNewDocument(){
		return this.textPaneNewDocument;
	}
	public JTextPane getJtextPaneDifferences(){
		return this.textPaneDifferences;
	}

	private void textPaneDifferencesHyperlinkUpdate(HyperlinkEvent e) {
		// TODO add your code here
	}
	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		ResourceBundle bundle = ResourceBundle.getBundle("locale");
		DefaultComponentFactory compFactory = DefaultComponentFactory.getInstance();
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		splitPane1 = new JSplitPane();
		panel3 = new JPanel();
		separator3 = compFactory.createSeparator(bundle.getString("separator3.text"));
		scrollPane3 = new JScrollPane();
		textPaneLastDocumentSaved = new JTextPane();
		labelVersionControlLNLast = new JLabel();
		panel1 = new JPanel();
		panel2 = new JPanel();
		separator2 = compFactory.createSeparator(bundle.getString("separator2.text"));
		scrollPane2 = new JScrollPane();
		textPaneNewDocument = new JTextPane();
		labelVersionControlNew = new JLabel();
		panel4 = new JPanel();
		separator4 = compFactory.createSeparator(bundle.getString("separator4.text"));
		scrollPane4 = new JScrollPane();
		textPaneDifferences = new JTextPane();
		buttonBar = new JPanel();
		okButton = new JButton();
		cancelButton = new JButton();
		labelVersionControl = new JLabel();

		//======== this ========
		setTitle(bundle.getString("this.title"));
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new BorderLayout());

				//======== splitPane1 ========
				{

					//======== panel3 ========
					{
						panel3.setLayout(new BorderLayout());
						panel3.add(separator3, BorderLayout.NORTH);

						//======== scrollPane3 ========
						{

							//---- textPaneLastDocumentSaved ----
							textPaneLastDocumentSaved.setPreferredSize(new Dimension(200, 200));
							textPaneLastDocumentSaved.setEditable(false);
							textPaneLastDocumentSaved.addCaretListener(new CaretListener() {
								public void caretUpdate(CaretEvent e) {
									textPaneLastDocumentSavedCaretUpdate(e);
								}
							});
							scrollPane3.setViewportView(textPaneLastDocumentSaved);
						}
						panel3.add(scrollPane3, BorderLayout.CENTER);

						//---- labelVersionControlLNLast ----
						labelVersionControlLNLast.setText(bundle.getString("labelVersionControlLNLast.text"));
						labelVersionControlLNLast.setForeground(Color.red);
						panel3.add(labelVersionControlLNLast, BorderLayout.SOUTH);
					}
					splitPane1.setLeftComponent(panel3);

					//======== panel1 ========
					{
						panel1.setLayout(new BorderLayout());

						//======== panel2 ========
						{
							panel2.setLayout(new BorderLayout());
							panel2.add(separator2, BorderLayout.NORTH);

							//======== scrollPane2 ========
							{

								//---- textPaneNewDocument ----
								textPaneNewDocument.setPreferredSize(new Dimension(200, 200));
								textPaneNewDocument.setEditable(false);
								textPaneNewDocument.addCaretListener(new CaretListener() {
									public void caretUpdate(CaretEvent e) {
										textPaneNewDocumentCaretUpdate(e);
									}
								});
								scrollPane2.setViewportView(textPaneNewDocument);
							}
							panel2.add(scrollPane2, BorderLayout.CENTER);

							//---- labelVersionControlNew ----
							labelVersionControlNew.setText(bundle.getString("labelVersionControlNew.text"));
							labelVersionControlNew.setForeground(Color.red);
							panel2.add(labelVersionControlNew, BorderLayout.SOUTH);
						}
						panel1.add(panel2, BorderLayout.CENTER);
					}
					splitPane1.setRightComponent(panel1);
				}
				contentPanel.add(splitPane1, BorderLayout.CENTER);

				//======== panel4 ========
				{
					panel4.setLayout(new BorderLayout());
					panel4.add(separator4, BorderLayout.NORTH);

					//======== scrollPane4 ========
					{
						scrollPane4.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

						//---- textPaneDifferences ----
						textPaneDifferences.setPreferredSize(new Dimension(270, 21));
						textPaneDifferences.addHyperlinkListener(new HyperlinkListener() {
							public void hyperlinkUpdate(HyperlinkEvent e) {
								textPaneDifferencesHyperlinkUpdate(e);
							}
						});
						scrollPane4.setViewportView(textPaneDifferences);
					}
					panel4.add(scrollPane4, BorderLayout.CENTER);
				}
				contentPanel.add(panel4, BorderLayout.EAST);
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
				buttonBar.setLayout(new GridBagLayout());
				((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
				((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

				//---- okButton ----
				okButton.setText(bundle.getString("okButton.text"));
				buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 5), 0, 0));

				//---- cancelButton ----
				cancelButton.setText(bundle.getString("cancelButton.text"));
				buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 0), 0, 0));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);

			//---- labelVersionControl ----
			labelVersionControl.setText(bundle.getString("labelVersionControl.text"));
			dialogPane.add(labelVersionControl, BorderLayout.NORTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JSplitPane splitPane1;
	private JPanel panel3;
	private JComponent separator3;
	private JScrollPane scrollPane3;
	private JTextPane textPaneLastDocumentSaved;
	private JLabel labelVersionControlLNLast;
	private JPanel panel1;
	private JPanel panel2;
	private JComponent separator2;
	private JScrollPane scrollPane2;
	private JTextPane textPaneNewDocument;
	private JLabel labelVersionControlNew;
	private JPanel panel4;
	private JComponent separator4;
	private JScrollPane scrollPane4;
	private JTextPane textPaneDifferences;
	private JPanel buttonBar;
	private JButton okButton;
	private JButton cancelButton;
	private JLabel labelVersionControl;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
